package com.example.cybz.sys.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cybz.common.result.ResultCodeEnum;
import com.example.cybz.common.untils.ExceptionThrower;
import com.example.cybz.model.sys.RegionWare;
import com.example.cybz.sys.mapper.RegionWareMapper;
import com.example.cybz.sys.service.RegionWareService;
import com.example.cybz.vo.sys.RegionWareQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 城市仓库关联表 服务实现类
 * </p>
 *
 * @author mhwssad
 * @since 2025-06-26
 */
@Service
public class RegionWareServiceImpl extends ServiceImpl<RegionWareMapper, RegionWare> implements RegionWareService {

    @Override
    public Page<RegionWare> selectPageRegionWar(Page<RegionWare> pageParam, RegionWareQueryVo regionWareQueryVo) {
        LambdaQueryChainWrapper<RegionWare> regionWareLambdaQueryChainWrapper = new LambdaQueryChainWrapper<>(baseMapper);
        if (StringUtils.hasText(regionWareQueryVo.getKeyword())){
            regionWareLambdaQueryChainWrapper
                    .like(RegionWare::getRegionName, regionWareQueryVo.getKeyword())
                    .or()
                    .like(RegionWare::getWareName, regionWareQueryVo.getKeyword());
        }
        return regionWareLambdaQueryChainWrapper.page(pageParam);
    }

    @Override
    public boolean saveRegionWare(RegionWare regionWare) {
        Integer count = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(RegionWare::getRegionId, regionWare.getRegionId()).count();
        ExceptionThrower.throwIf(count > 0, ResultCodeEnum.DATA_ALREADY_EXISTS);
        return save(regionWare);
    }

    @Override
    public boolean updateByStatus(Long id, Integer status) {
        return new LambdaUpdateChainWrapper<>(baseMapper)
                .eq(RegionWare::getId, id)
                .set(RegionWare::getStatus, status)
                .update();
    }
}
